package com.kingdee.hackson.controller;

import com.kingdee.hackson.entity.HelloWorld;
import com.kingdee.hackson.service.IHelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/helloworld")
public class HelloWorldController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private IHelloWorldService helloWorldService;

    //http://localhost:8080/helloworld/echo/dfsdfdsf
    @GetMapping("/echo/{msg}")
    public String hello(@PathVariable String msg) {
        return msg;
    }

    @GetMapping("/list")
    public List<HelloWorld> hello() {
        return helloWorldService.getList();
    }

    /**
     *
     * @return
     */
    @GetMapping("/jdbc")
    public List<HelloWorld> jdbc() {
        List<HelloWorld> query = jdbcTemplate.query("select id,name,number from hello_world_t ", new RowMapper<HelloWorld>() {
            @Override
            public HelloWorld mapRow(ResultSet resultSet, int i) throws SQLException {
                long id = resultSet.getLong(1);
                String name = resultSet.getString(2);
                String number = resultSet.getString(3);
                HelloWorld helloWorld = new HelloWorld();
                helloWorld.setId(id);
                helloWorld.setName(name);
                helloWorld.setNumber(number);
                return helloWorld;
            }
        });
        return query;
    }

}
